package com.harboursoftware.xstorage.util;

import java.util.Iterator;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.harboursoftware.xstorage.XStorageRuntimeException;

public class Configuration {
    private final static Logger log = LoggerFactory.getLogger(Configuration.class);
    /**
     * The name of configuration file on class path.
     */
    private final static String CONFIGURATION_FILE_NAME = "xstorage.config";
    private static Configuration instance = new Configuration();
    
    private static PropertiesConfiguration config;
    
    static {
        initConfig();
    }
    
    private static void initConfig() {
        try {
            config = new PropertiesConfiguration(CONFIGURATION_FILE_NAME);
            log.info("loading configuration from file '{}' on classpath", CONFIGURATION_FILE_NAME);
            Iterator keys = config.getKeys();
            while(keys.hasNext()) {
                String key = (String)keys.next();
                String value = config.getString(key);
                log.info("{} : {}", key, value);
            }
        } catch (ConfigurationException e) {
            throw new XStorageRuntimeException("fail to load configuration file '" + CONFIGURATION_FILE_NAME + "' on classpath", e);
        }
    }
    /**
     * singleton
     */
    private Configuration() {  
    }
    
    public static Configuration getInstance() {
        return instance;
    }
    
    public String getString(String key) {
        return config.getString(key);
    }
    
    public int getInt(String key) {
        return config.getInt(key);
    }
}
